﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns
{
    public interface ISubject
    {

        void Request();
    }
    /// <summary>
    /// 代理的对象  
    /// 在代理类中提前声明并通过构造函数实例化
    /// </summary>
    public class RealSubject : ISubject
    {
        public void Request()
        {
            Console.WriteLine("打印原始的RealSubject");
        }
    }

    /// <summary>
    /// 代理类 ，实现了ISbuject接口
    /// </summary>
    public class Proxy : ISubject
    {
        private RealSubject _realSubject;
        public Proxy()
        {
            this._realSubject = new RealSubject();
        }

        public void Request()
        {
            if (CheakAccess())
            {
                _realSubject.Request();
                LogAcess();
            }
        }

        private bool CheakAccess()
        {
            Console.WriteLine("Proxy:类  检测验证");
            return true;
        }

        private void LogAcess()
        {
            Console.WriteLine("Proxy类，主动输出LogAcess");
        }
    }

    /// <summary>
    /// 验证的客户端
    /// 可接收实现了ISubject接口的类
    /// </summary>
    public class ProxPatternClient
    {
        public void ClientCode(ISubject subject)
        {
            subject.Request();
        }
    }

}
